import os
from config.setting import API_CASE_PATH
from lib.config_parse import CaseFileRequest

from lib.error_log import ErrorLog
from pathlib import Path
log = ErrorLog().get_log()


def create(yaml_path):
    """创建scripts单接口脚本的template文件"""
    case = CaseFileRequest(yaml_path)
    file_name = case.get_case_suite() + '.py'
    case_file_name = os.path.join(API_CASE_PATH, file_name)
    print(case_file_name)
    Temaplate = f"""
import pytest
import allure
from lib.config_parse import call_case

@allure.feature('{case.get_case_feature()}')
class {case.get_case_module_class()}:
"""
    examle = f"""
    @call_case(r'{yaml_path}')
    def {case.get_case_suite()}(self):
    pass
"""
    debug_info = """
if __name__ =="__main__":
    pytest.main()
"""
    Temaplate += examle
    Temaplate += debug_info
    with open(case_file_name, 'a', encoding='utf-8') as f:
        f.write(Temaplate)

def create_par_case(yaml_par_path):
    """创建scripts参数化脚本的template文件"""
    case = CaseFileRequest(yaml_par_path)
    data  = CaseFileRequest(yaml_par_path).get_params_data()
    file_name = case.get_case_suite() + '.py'
    case_file_name = os.path.join(API_CASE_PATH, file_name)
    print(case_file_name)
    Temaplate = f"""
import pytest
import allure
import os
from config.setting import DATA_YAML_PATH
from lib.config_parse import ConfigParser,CaseFileRequest
from lib.http_cli import HttpCli
from utils.get_encrypt_utils import encrypt_xor_param

@allure.feature('{case.get_case_feature()}')
class {case.get_case_module_class()}:
"""
    dir = f"""
    data = {data}
    """
    example = f"""
    @pytest.mark.parametrize('dic',data)
    def {case.get_case_suite()}(self,dic):
        path='{yaml_par_path}'
        self.host = ConfigParser().market_base_url()
        self.case_path = CaseFileRequest(path).get_case_path()
        self.method = CaseFileRequest(path).get_case_method()
        self.headers = CaseFileRequest(path).get_case_headers()
        self.requests_params = dic
        self.requests_data = encrypt_xor_param(self.requests_params,
                                                CaseFileRequest(path).get_domain(),
                                                CaseFileRequest(path).get_sign_type())


        return HttpCli(self.headers).exec_request(method = self.method,
                                                    url = self.host + self.case_path,
                                                    data = self.requests_data)

    """
    debug_info = """
if __name__ =='__main__':
    pytest.main()
    """
    Temaplate += dir
    Temaplate += example
    Temaplate += debug_info
    with open(case_file_name,'a', encoding='utf-8') as f:
        f.write(Temaplate)

def remove_case():
    """删除测试用例方法"""
    if API_CASE_PATH and os.path.exists(API_CASE_PATH):
        p = Path(API_CASE_PATH)
        for file in p.rglob("*.py"):
            name = file.name.split(".")[0]
            if name != "__init__":
                log.info(f"删除文件{file.name}")
                os.remove(file)


if __name__ =='__main__':
    r =r'E:\msshuo-test\data\single\test_apply_join.yaml'
    # create(r)s
    create_par_case(r)
    # remove_case(r)